package com.ruoyi.binance.service;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.binance.api.OrderApi;
import com.ruoyi.binance.domain.*;
import com.ruoyi.binance.mapper.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

@Component
public class AsyncAveBuySellPlus {

    @Autowired
    private BinanceMinKMapper binanceMinKMapper;
    @Autowired
    private Binance15minAverageMapper binance15minAverageMapper;
    @Autowired
    private Binance30minAverageMapper binance30minAverageMapper;
    @Autowired
    private BinanceHourAverageMapper binanceHourAverageMapper;
    @Autowired
    private BinanceTransactionOrderMapper binanceTransactionOrderMapper;
    @Autowired
    private BinanceTransactionConfMapper binanceTransactionConfMapper;

    @Async
    public void getAveragePrice(Long confId, String code, Date time, BigDecimal close, BigDecimal open) {
        //根据监控均价类型赋值均价
        BinanceHourAverage binanceHourAverage = binanceHourAverageMapper.selectBinanceHourAverageByCodeAndTime(code, DateUtils.getFormatTime(time));
        Binance30minAverage binance30minAverage = binance30minAverageMapper.selectBinance30minAverageByCodeAndTime(code, DateUtils.getFormatTime(time));
        Binance15minAverage binance15minAverage = binance15minAverageMapper.selectBinance15minAverageByCodeAndTime(code, DateUtils.getFormatTime(time));
        BinanceMinK lastMinK = binanceMinKMapper.selectBinanceMinKByCodeAndTime(code, DateUtils.getFormatTime(DateUtils.addMinutes(time, -1)));
        //close < 30分均价
        if (close.compareTo(binance30minAverage.getPrice()) < 0) {
            //触发止损
            BinanceTransactionConf binanceTransactionConf = binanceTransactionConfMapper.selectBinanceTransactionConfById(confId);
            if (binanceTransactionConf != null && binanceTransactionConf.getCoinNumber().compareTo(BigDecimal.ZERO) > 0
                    && !"SELL".equals(binanceTransactionConf.getSide())) {
                BinanceTransactionConf bean = new BinanceTransactionConf();
                bean.setId(binanceTransactionConf.getId());
                bean.setSide("SELL");
                binanceTransactionConfMapper.updateBinanceTransactionConf(bean);

                BinanceTransactionOrder order = new BinanceTransactionOrder();
                order.setCode(code);
                order.setOrgOrderId(IdUtil.getSnowflakeNextIdStr());
                order.setQuantity(binanceTransactionConf.getCoinNumber());
                order.setSide("SELL");
                order.setType("MARKET");
                order.setConfigId(confId);
                order.setCreateTime(time);
                order.setRemark("自动交易");
                int save = binanceTransactionOrderMapper.insertBinanceTransactionOrder(order);
                if (save > 0) {
                    //调用下单接口
                    JSONObject res = OrderApi.order(order);
                    if (StringUtils.isNotBlank(res.getString("code"))) {
                        order.setStatus("REJECTED");
                        order.setRemark(res.getString("msg"));
                    } else {
                        order.setStatus("PENDING_NEW");
                        order.setOrderId(res.getString("orderId"));
                    }
                    binanceTransactionOrderMapper.updateBinanceTransactionOrder(order);
                }
            }
            return;
        }
        //30分均价 > 时均价 为多行情
        if (binance30minAverage.getPrice().compareTo(binanceHourAverage.getPrice()) > 0) {
            //close > 30分均价 && close <= 15分均价 && close > 上次收盘价
            if (close.compareTo(binance30minAverage.getPrice()) > 0 && close.compareTo(binance15minAverage.getPrice()) <= 0
                    && close.compareTo(lastMinK.getClose()) > 0 && close.compareTo(open) > 0) {
                //进行下单
                BinanceTransactionConf binanceTransactionConf = binanceTransactionConfMapper.selectBinanceTransactionConfById(confId);
                if (binanceTransactionConf != null && binanceTransactionConf.getUsdtNumber().compareTo(BigDecimal.ZERO) > 0
                        && !"BUY".equals(binanceTransactionConf.getSide())) {
                    BinanceTransactionConf bean = new BinanceTransactionConf();
                    bean.setId(binanceTransactionConf.getId());
                    bean.setSide("BUY");
                    binanceTransactionConfMapper.updateBinanceTransactionConf(bean);

                    BinanceTransactionOrder order = new BinanceTransactionOrder();
                    order.setCode(code);
                    order.setOrgOrderId(IdUtil.getSnowflakeNextIdStr());
                    order.setQuoteOrderQty(binanceTransactionConf.getUsdtNumber());
                    order.setSide("BUY");
                    order.setType("MARKET");
                    order.setConfigId(confId);
                    order.setCreateTime(time);
                    order.setRemark("自动交易");
                    int save = binanceTransactionOrderMapper.insertBinanceTransactionOrder(order);
                    if (save > 0) {
                        //调用下单接口
                        JSONObject res = OrderApi.order(order);
                        if (StringUtils.isNotBlank(res.getString("code"))) {
                            order.setStatus("REJECTED");
                            order.setRemark(res.getString("msg"));
                        } else {
                            order.setStatus("PENDING_NEW");
                            order.setOrderId(res.getString("orderId"));
                        }
                        binanceTransactionOrderMapper.updateBinanceTransactionOrder(order);
                    }
                }
            }
        }
        // close <= 时均价
        if (close.compareTo(binanceHourAverage.getPrice()) <= 0) {
            //进行下单
            BinanceTransactionConf binanceTransactionConf = binanceTransactionConfMapper.selectBinanceTransactionConfById(confId);
            if (binanceTransactionConf != null && binanceTransactionConf.getCoinNumber().compareTo(BigDecimal.ZERO) > 0
                    && !"SELL".equals(binanceTransactionConf.getSide())) {

                BinanceTransactionConf bean = new BinanceTransactionConf();
                bean.setId(binanceTransactionConf.getId());
                bean.setSide("SELL");
                binanceTransactionConfMapper.updateBinanceTransactionConf(bean);

                BinanceTransactionOrder order = new BinanceTransactionOrder();
                order.setCode(code);
                order.setOrgOrderId(IdUtil.getSnowflakeNextIdStr());
                order.setQuantity(binanceTransactionConf.getCoinNumber());
                order.setSide("SELL");
                order.setType("MARKET");
                order.setConfigId(confId);
                order.setCreateTime(time);
                order.setRemark("自动交易");
                int save = binanceTransactionOrderMapper.insertBinanceTransactionOrder(order);
                if (save > 0) {
                    //调用下单接口
                    JSONObject res = OrderApi.order(order);
                    if (StringUtils.isNotBlank(res.getString("code"))) {
                        order.setStatus("REJECTED");
                        order.setRemark(res.getString("msg"));
                    } else {
                        order.setStatus("PENDING_NEW");
                        order.setOrderId(res.getString("orderId"));
                    }
                    binanceTransactionOrderMapper.updateBinanceTransactionOrder(order);
                }
            }
        }
    }
}
